package com.example.service.impl;

import com.example.mapper.MessageMapper;
import com.example.pojo.VO.Result;
import com.example.pojo.entrity.Message;
import com.example.service.MessageService;
import com.example.utils.CreateID;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static java.time.LocalTime.now;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Result sendMessage(String test,String product_id) {
        String id= CreateID.createUniqueID();
//        从线程中获取当前用户登录的id
        Map<String,Object> map= ThreadLocalUtil.get();
        String userId= (String) map.get("userId");
        Message msg=new Message(id,userId, product_id,test,now());
        messageMapper.insertMessage(msg);
        return Result.success("发布成功");
    }

    @Override
    public Result deleteMessage(String id) {
        messageMapper.deleteMessage(id);
        return Result.success("删除成功");
    }

    @Override
    public Result getMessages() {
//        TODO 后期更改数据库，目前所有留言全部获取出来
        List<Message> list=messageMapper.getMessages();

        return Result.success(list);
    }
}
